package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WmchannelServicesImpl implements WmChannelService {

    @Autowired
    private WmChannelMapper wmchannelMapper;

    @Autowired
    private WmNewsMapper wmNewsMapper;


    @Override
    public ResponseResult getAllChannel() {
        List<WmChannel> wmChannels = wmchannelMapper.selectList(null);

        return ResponseResult.okResult(wmChannels);
    }

    /**
     * 查询频道列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findAll(ChannelDto dto) {
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        dto.checkParam();
        Page<WmChannel> page = new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(WmChannel::getName,dto.getName());
        }
        lambdaQueryWrapper.orderByDesc(WmChannel::getCreatedTime);

        wmchannelMapper.selectPage(page,lambdaQueryWrapper);

        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        responseResult.setData(page.getRecords());

        return responseResult;
    }

    /**
     * 添加频道
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult addWmChannel(WmChannel wmChannel) {
        if (wmChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Integer selectCount = wmchannelMapper.selectCount(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, wmChannel.getName()));

        if (selectCount > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        wmChannel.setCreatedTime(new Date());

        wmChannel.setIsDefault(true);

        wmchannelMapper.insert(wmChannel);

        return ResponseResult.okResult(wmChannel);
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        // 被引用的频道是不能删除的
        Integer selectCount = wmNewsMapper.selectCount(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, id));
        if (selectCount > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_CITED);
        }
        wmchannelMapper.deleteById(id);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改频道
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult update(WmChannel wmChannel) {
        // 被引用的频道是不能修改的
        Integer selectCount1 = wmNewsMapper.selectCount(Wrappers.<WmNews>lambdaQuery().eq(WmNews::getChannelId, wmChannel.getId()));
        if (selectCount1 > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_CITED);
        }

        Integer selectCount2 = wmchannelMapper.selectCount(Wrappers.<WmChannel>lambdaQuery().eq(WmChannel::getName, wmChannel.getName()));
        if (selectCount2 > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道名称重复");
        }
        wmChannel.setCreatedTime(new Date());
        wmChannel.setIsDefault(true);
        wmchannelMapper.updateById(wmChannel);
        return ResponseResult.okResult(wmChannel);
    }
}
